﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ForrajeriaBCL;
using System.Data;
namespace ForrajeriaFRONT.Reportes
{
    public partial class ForrajesStock : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            chkStockminimo.Attributes.Add("onclick", "enableField();");
            if (!Page.IsPostBack)
            {

                ForrajeBCL forraBCL = new ForrajeBCL();
                DataTable dt = new DataTable();
                try
                {
                    dt = forraBCL.reporteForrajeStock();
                    
                }
                catch
                {
                    Response.Redirect("/Default.aspx");
                }
                
                              

                gridProveedor.DataSource = dt;
                gridProveedor.DataBind();
                ViewState["lista"] = dt;
                ViewState["columna"] = "Codigo_Forraje";
                ViewState["direccion"] = "asc";
            }
        }

        protected void btnFiltrar_Click(object sender, EventArgs e)
        {
            if (chkStockminimo.Checked)
            {
                txtStock.Enabled = false;
                chkStockminimo.Checked = true;
            }
            else
            {
                txtStock.Enabled = true;
            }
            cargarGrilla();
        }


        protected void cargarGrilla()
        {
            ForrajeBCL forrBCL = new ForrajeBCL();
            DataTable dt = new DataTable();
            double stock=0;
            
            if(!double.TryParse(txtStock.Text, out stock))
                stock=0;
            try
            {
                //if()
                    dt = forrBCL.reporteForrajeStock(txtForraje.Text,stock , chkStockminimo.Checked, ViewState["columna"].ToString(), ViewState["direccion"].ToString());
                //else
                    //dt = forrBCL.reporteForrajeStock(txtForraje.Text, stock, true, ViewState["columna"].ToString(), ViewState["direccion"].ToString());
                ViewState["lista"] = dt;
                gridProveedor.DataSource = dt;
                gridProveedor.DataBind();


            }
            catch
            {
                Response.Redirect("/Default.aspx");
            }
        }


        protected void gridProveedor_PageIndexChanged(object sender, EventArgs e)
        {
            

        }

        protected void gridProveedor_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridProveedor.DataSource = (DataTable)ViewState["lista"];
            gridProveedor.PageIndex = e.NewPageIndex;
            gridProveedor.DataBind();
        }

        protected void gridProveedor_Sorting(object sender, GridViewSortEventArgs e)
        {
            ViewState["columna"] = e.SortExpression;

            if (ViewState["direccion"].ToString() == "asc")
            {
                ViewState["direccion"] = "desc";
            }
            else
            {
                ViewState["direccion"] = "asc";
            }

            cargarGrilla();
          
            
        }

     
    }
}